﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TextRPG.Models;
using TextRPG.Models.ViewModels;

namespace TextRPG.Controllers
{ 
    public class ItemController : Controller
    {
        private RPGContext db = new RPGContext();

        //
        // GET: /Item/

        public ViewResult Index()
        {
            var items = db.Items.Include(i => i.ItemDefinition);
            return View(items.ToList());
        }

        //
        // GET: /Item/Details/5

        public ViewResult Details(int id)
        {
            Item item = db.Items.Find(id);

            //finder alle modfiers og adder dem til itemet med AttachedModifier view modellen
            List<ItemModifierRelation> iir = db.ItemModifierRelations.Where(x => x.ItemID == id).ToList();
            AttachedModifier attachedModifier;
            Modifier modifier;
            foreach (ItemModifierRelation i in iir)
            {
                attachedModifier = new AttachedModifier();
                modifier = db.Modifiers.Where(x => x.ModifierID == i.ModifierID).Single();

                attachedModifier.Name = modifier.Name;
                attachedModifier.postfix = modifier.postfix;
                attachedModifier.value = i.value;
                attachedModifier.ModifierID = i.ModifierID;

                item.AttachedModifiers.Add(attachedModifier);
            }

            return View(item);
        }

        //
        // GET: /Item/Create

        public ActionResult Create()
        {
            ViewBag.ItemDefinitionID = new SelectList(db.ItemDefinitions, "ItemDefinitionID", "Name");
            return View();
        } 

        //
        // POST: /Item/Create

        [HttpPost]
        public ActionResult Create(Item item)
        {
            if (ModelState.IsValid)
            {
                db.Items.Add(item);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.ItemDefinitionID = new SelectList(db.ItemDefinitions, "ItemDefinitionID", "Name", item.ItemDefinitionID);
            return View(item);
        }
        
        //
        // GET: /Item/Edit/5
 
        public ActionResult Edit(int id)
        {

            Item item = db.Items.Find(id);

            //finder alle modfiers og adder dem til itemet med AttachedModifier view modellen
            List<ItemModifierRelation> iir = db.ItemModifierRelations.Where(x => x.ItemID == id).ToList();
            AttachedModifier attachedModifier;
            Modifier modifier;
            foreach (ItemModifierRelation i in iir)
            {
                attachedModifier = new AttachedModifier();
                modifier = db.Modifiers.Where(x => x.ModifierID == i.ModifierID).Single();

                attachedModifier.Name = modifier.Name;
                attachedModifier.postfix = modifier.postfix;
                attachedModifier.value = i.value;
                attachedModifier.ModifierID = i.ItemModifierRelationID;

                item.AttachedModifiers.Add(attachedModifier);
            }

            ViewBag.ItemDefinitionID = new SelectList(db.ItemDefinitions, "ItemDefinitionID", "Name", item.ItemDefinitionID);
            return View(item);
        }

        //
        // POST: /Item/Edit/5

        [HttpPost]
        public ActionResult Edit(Item item)
        {
            if (ModelState.IsValid)
            {
                db.Entry(item).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ItemDefinitionID = new SelectList(db.ItemDefinitions, "ItemDefinitionID", "Name", item.ItemDefinitionID);
            return View(item);
        }

        //
        // GET: /Item/Delete/5

        public ActionResult Delete(int id)
        {
            Item item = db.Items.Find(id);
            return View(item);
        }

        //
        // POST: /Item/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Item item = db.Items.Find(id);
            db.Items.Remove(item);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        //
        // GET: /Item/Create

        public ActionResult AddModifier(int id)
        {
            ViewBag.ModifierID = new SelectList(db.Modifiers, "ModifierID", "Name");
            ItemModifierRelation imr = new ItemModifierRelation();
            imr.ItemID = id;
            return View(imr);
        }

        //
        // POST: /Item/Create

        [HttpPost]
        public ActionResult AddModifier(ItemModifierRelation modifier, string redirection, int redirectionValue)
        {
            if (ModelState.IsValid)
            {
                db.ItemModifierRelations.Add(modifier);
                db.SaveChanges();
                return RedirectToAction(redirection, new { id = redirectionValue });
            }

            ViewBag.Modifiers = new SelectList(db.Modifiers, "ModifierID", "Name", modifier.ModifierID);
            return View(modifier);
        }

        //
        // GET: /Item/Delete/5

        public ActionResult RemoveModifier(int id)
        {
            ItemModifierRelation item = db.ItemModifierRelations.Find(id);
            return View(item);
        }

        //
        // POST: /Item/Delete/5

        [HttpPost, ActionName("RemoveModifier")]
        public ActionResult RemoveModifierConfirmed(int id, string redirection, int redirectionValue)
        {
            ItemModifierRelation item = db.ItemModifierRelations.Find(id);
            db.ItemModifierRelations.Remove(item);
            db.SaveChanges();
            return RedirectToAction(redirection, new { id = redirectionValue });
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}